[#import "/framework/JGP.htm"  as JGP]
<!--
项目   jgp-cloud-parent
作者   LOUFEI [Gavin]
时间   2019-09-05
-->
[@JGP.head "属性排序"]
    <style>
        .sort-input{
            height: 100%;
            width: 50px;
            outline: none;
            border-bottom: 1px dotted gray;
            text-align: center;
        }
    </style>
[/@JGP.head]
[@JGP.body]
    <jgp-div _row="['30','*']" _runit="px">
        <div slot="1-1">
            <jgp-btn _fn="saveSort">保存</jgp-btn>
            <jgp-btn _fn="saveSortAndClose">保存并关闭</jgp-btn>
        </div>

        <jgp-grid _id="attr-grid" slot="2-1" _onload="doGridLoadedFn" _url="/form/formApiController/queryFormAttrForSort/${formId!''}">
            <jgp-grid-col _name="sort" _fns="['onSorted']" _fns_mode="change" _wrap="<input class='sort-input' type='text' value='[content]'>"></jgp-grid-col>
        </jgp-grid>
    </jgp-div>

[/@JGP.body]

[@JGP.foot]
    <script>
        var changedAttr = {}

        function onSorted(args) {
            var record = args.back.record;
            var value = args.back.dom.value;
            changedAttr[record.id] = value;
        }

        function saveSort() {
            JGP.$postJson('/form/formApiController/configFormAttr',{
                sortMap:changedAttr,
                inGridsMap:getAllInGrid()
            },function () {
                location.reload();
            })
        }

        function getAllInGrid() {
            var grid = JGP.grid('attr-grid');
            var records = grid.getAllRecords();
            var selected = grid.getSelected();
            var selectedKeys = [];
            var result = {};
            for (var i = 0; i < selected.length; i++) {
                selectedKeys.push(selected[i].id)
            }
            for (var i = 0; i < records.length; i++) {
                var record = records[i];
                if(selectedKeys.indexOf(record.id)!=-1){
                    result[record.id] = true;
                }else{
                    result[record.id] = false;
                }
            }
            return result;
        }

        function saveSortAndClose() {
            JGP.$postJson('/form/formApiController/configFormAttr',{
                sortMap:changedAttr,
                inGridsMap:getAllInGrid()
            },function () {
                JGP.$close(true);
            })
        }

        function doGridLoadedFn() {
            var grid = JGP.grid('attr-grid');
            var records = grid.getAllRecords();
            var selected = [];
            for (var i = 0; i < records.length; i++) {
                var record = records[i];
                if(record.inGrid){
                    selected.push(record.id);
                }
            }
            grid.setSelected(selected);
        }
    </script>
[/@JGP.foot]